package com.xbb.controller.sys;

import com.xbb.common.RespResult;
import com.xbb.constant.SessionConstant;
import com.xbb.entity.User;
import com.xbb.mapper.UserMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/sys/user")
public class SysUserController {

    private String prefix = "/sys/user/";

    @Resource
    private UserMapper userMapper;

    // 用户管理
    @RequestMapping("/manage.html")
    public String manageHtml () {
        return prefix + "user";
    }

    @ResponseBody
    @RequestMapping("/data")
    public RespResult data (@RequestParam("page") int page,
                            @RequestParam("limit") int limit,
                            @RequestParam(value = "name", required = false, defaultValue = "") String name,
                            @RequestParam(value = "sex", required = false) Integer sex,
                            @RequestParam(value = "role", required = false) Integer role) {
        List<User> data = userMapper.selectListByPaging(
                (page - 1) * limit,
                limit,
                name,
                sex,
                role
        );
        int count = userMapper.selectCountByPaging(
                name,
                sex,
                role
        );
        RespResult respResult = new RespResult();
        respResult.success(data, count);
        return respResult;
    }

    // 添加
    @ResponseBody
    @RequestMapping("/add")
    public RespResult add(@RequestBody User user) {
        RespResult respResult = new RespResult();
        // 查询是否存在用户
        User u = userMapper.selectByUsername(user.getUsername());
        if (u == null) {
            userMapper.insertSelective(user);    // 不存在则插入
        } else {
            respResult.setErrorMessage("用户名已存在，请重新输入");
        }
        return respResult;
    }

    // 编辑
    @ResponseBody
    @RequestMapping("/edit")
    public RespResult edit(@RequestBody User user) {
        RespResult respResult = new RespResult();
        // 查询是否已经存在此用户
        User u = userMapper.selectByUsername(user.getUsername());
        // 不存在则修改
        if (u == null) {
            userMapper.updateByPrimaryKeySelective(user);
        } else {
            // 存在的话比较ID是否一致，一致则修改（防止用户名冲突）
            if (u.getId() == user.getId()) {
                userMapper.updateByPrimaryKeySelective(user);
            } else {
                respResult.setErrorMessage("用户名已存在，请重新输入");
            }
        }
        return respResult;
    }

    // 删除
    @ResponseBody
    @RequestMapping("/delete")
    public RespResult delete(@RequestBody Integer id) {
        userMapper.deleteByPrimaryKey(id);
        return new RespResult();
    }

    // 编辑个人资料
    @RequestMapping("/personal.html")
    public String personalHtml () {
        return prefix + "personal";
    }

    // 重置密码
    @RequestMapping("/password.html")
    public String passwordHtml () {
        return prefix + "password";
    }

    @ResponseBody
    @RequestMapping("/personal/edit")
    public RespResult editPersonal(@RequestBody User user, HttpSession session) {
        userMapper.updateByPrimaryKeySelective(user);
        // 更新 session
        User u = userMapper.selectByPrimaryKey(user.getId());
        session.removeAttribute(SessionConstant.KEY_USER);
        session.setAttribute(SessionConstant.KEY_USER, u);
        return new RespResult();
    }

}

